package com.doudou.oauth.service;

import com.doudou.client.BasicClient;
import com.doudou.client.entity.UserAccountInfoDto;
import com.doudou.client.entity.UserDetailsInfoDto;
import com.doudou.commons.entity.Result;
import com.doudou.commons.utils.ClientResultUtils;
import com.doudou.commons.utils.MyAssert;
import com.doudou.oauth.entity.AuthUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;

/**
 * @Author: 傻男人
 * @Date: 2020/5/15 11:28
 * @Version: 1.0
 * @Description:
 */
@Service
public class MyUserDetailsService implements UserDetailsService,AppUserRoleService{

    @Autowired
    private BasicClient basicClient;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return loadUser(username);
    }

    private AuthUserDto loadUser(String username) {
        UserAccountInfoDto userAccountInfoDto = findByUsername(username);
        Result<UserDetailsInfoDto> userDetailsInfoDtoResult = basicClient.findUserDetailsInfoByUserId(userAccountInfoDto.getId());
        UserDetailsInfoDto userDetailsInfoDto = ClientResultUtils.getResultIfErrorException(userDetailsInfoDtoResult,"远程查询用户详情失败，用户的id为："+userAccountInfoDto.getId());
        AuthUserDto authUserDto = new AuthUserDto(username,userAccountInfoDto.getPassword(),userDetailsInfoDto,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,root")
        );
        PreAuthenticatedAuthenticationToken authentication =
                new PreAuthenticatedAuthenticationToken(authUserDto, authUserDto.getPassword(),authUserDto.getAuthorities());
        //存放authentication到SecurityContextHolder
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return authUserDto;
    }

    public AuthUserDto loadUserByPhone(String phone) {
        UserAccountInfoDto userAccountInfoDto = findByUsername(phone);
        Result<UserDetailsInfoDto> userDetailsInfoDtoResult = basicClient.findUserDetailsInfoByUserId(userAccountInfoDto.getId());
        UserDetailsInfoDto userDetailsInfoDto = ClientResultUtils.getResultIfErrorException(userDetailsInfoDtoResult,"远程查询用户详情失败，用户的id为："+userAccountInfoDto.getId());
        return new AuthUserDto(phone,userDetailsInfoDto,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,root")
        );
    }

    private UserAccountInfoDto findByUsername(String username){
        Result<UserAccountInfoDto> userAccountInfoDtoResult = basicClient.findByUsername(username);
        UserAccountInfoDto userAccountInfoDto = ClientResultUtils.getResultIfErrorNull(userAccountInfoDtoResult);
        MyAssert.notNull(userAccountInfoDto,"用户名或者密码错误");
        return userAccountInfoDto;
    }

    /**
     * 加载应用用户授权信息
     *
     * @param appId    应用id
     * @param userName 用户名
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> loadRole(String appId, String userName) {
        return null;
    }

    /**
     * 加载应用用户授权信息
     *
     * @param appId  应用id
     * @param userId 用户的id
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> loadRole(String appId, Long userId) {
        return null;
    }

    @Override
    public Map<String, Object> loadUserByAppCode(String appCode) {
        return null;
    }
}
